package com.wannengbang.storemobile.homepage;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.wannengbang.storemobile.R;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.wannengbang.storemobile.utils.SdUtlis;
import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.UserInfoBean;
import com.wannengbang.storemobile.bean.WechatStateBean;
import com.wannengbang.storemobile.bean.WechatVxBean;
import com.wannengbang.storemobile.mine.model.MineModelImpl;
import com.wannengbang.storemobile.utils.Base64BitmapUtil;
import com.wannengbang.storemobile.utils.GlideUtils;
import com.wannengbang.storemobile.utils.SPManager;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.AppTitleBar;
import com.wannengbang.storemobile.widget.ViewLoading;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WeiXinActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_merchant_no)
    TextView tvMerchantNo;
    @BindView(R.id.tv_fz)
    TextView tv_fz;
    @BindView(R.id.tv_ms)
    TextView tv_ms;
    @BindView(R.id.iv_dianyin_photo)
    ImageView ivDianyinPhoto;
    private MineModelImpl mineModel = new MineModelImpl();
    private String thirdMercId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xin);
        ButterKnife.bind(this);
        initData();
        setEvent();
    }

    private void setEvent() {
        ivDianyinPhoto.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Bitmap   bitmap = createViewBitmap(ivDianyinPhoto);
                saveImageToGallery(WeiXinActivity.this, bitmap);
                return true;
            }
        });
        tv_fz.setOnClickListener(v -> {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", thirdMercId);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.showShort("复制成功");
        });
    }

    public void initData() {
        mineModel.getVx(new DataCallBack<WechatVxBean>() {
            @Override
            public void onSuccessful(WechatVxBean bean) {
                thirdMercId = bean.getData().getWx_merchant_no().getThirdMercId();
                tvMerchantNo.setText("微信商户号：" +thirdMercId);
                tv_fz.setText("复制" );
                tv_ms.setText("（请复制微信商户号去微信认证）" );

                Glide.with(WeiXinActivity.this)
                        .load( bean.getData().getQr_url())
                        .centerCrop()
                        .placeholder(R.mipmap.ic_no_priture)//图片加载出来前，显示的图片
                        .error(R.mipmap.ic_no_priture)//图片加载失败后，显示的图片
                        .into(ivDianyinPhoto);

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
    public Bitmap createViewBitmap(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }
    public void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //其次把文件插入到系统图库
            String insertImage = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
            File file1 = new File(SdUtlis.getRealPathFromURI(Uri.parse(insertImage), context));
            SdUtlis.updatePhotoMedia(file1, WeiXinActivity.this);
            Toast.makeText(context, "图片已保存到本地相册", Toast.LENGTH_SHORT).show();
            finish();
        } catch (Exception e) {
            Toast.makeText(context, "请检查是否开启相册权限", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}